/* the official errno cli tool is in moreutils
 * this is just a imitation, please see 'man errno' for details
 */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define ERRNO_SUM 133

// functions
static void
usage(char *cmd)
{
  printf("Usage: %s or %s [error number(1 - %ud)]\n", cmd, cmd, ERRNO_SUM);
}

static int
isstrdecimal(char *str)
{
  while (*str) {
    if (!isdigit(*str)) {
      return 0;
    }
    str++;
  }
  return 1;
}

int main(int argc, char *argv[])
{
  // from number 1 to ERRNO_SUM, every errno coresponds to a error
  // number 0 is Success, it's not a error
  // == see 'man errno' for more details ==

  switch (argc) {
    case 1:
      for (int i = 1; i <= ERRNO_SUM; i++) {
        printf("[%d] %s\n", i, strerror(i));
      }
      break;
    case 2:
      if (!isstrdecimal(argv[1])) {
        fprintf(stderr, "%s: second argument must be a unsigned integer\n", argv[0]);
        break;
      }
      printf("[%s] %s\n", argv[1], strerror(atoi(argv[1])));
      break;
    default:
      usage(argv[0]);
      break;
  }

  return 0;
}
